package com.cyzy.controller;

import com.cyzy.entity.Course;
import com.cyzy.service.CourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    /**
     * 新增课程
     */
    @PostMapping("/add")
    public Map<String, Object> addCourse(@RequestBody Course course) {
        Map<String, Object> result = new HashMap<>();
        try {
            // 计算dateDay（格式：月*100+日，如5月8日为508）
            if (course.getDate() != null && !course.getDate().isEmpty()) {
                String[] parts = course.getDate().split("-");
                if (parts.length == 3) {
                    int month = Integer.parseInt(parts[1]);
                    int day = Integer.parseInt(parts[2]);
                    course.setDateDay(month * 100 + day);
                }
            }

            int rows = courseService.addCourse(course);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "课程添加成功");
                result.put("data", course);
            } else {
                result.put("success", false);
                result.put("message", "课程添加失败");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "服务器错误：" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 更新课程
     */
    @PostMapping("/update")
    public Map<String, Object> updateCourse(@RequestBody Course course) {
        Map<String, Object> result = new HashMap<>();
        try {
            if (course.getId() == null) {
                result.put("success", false);
                result.put("message", "课程ID不能为空");
                return result;
            }

            // 更新dateDay
            if (course.getDate() != null && !course.getDate().isEmpty()) {
                String[] parts = course.getDate().split("-");
                if (parts.length == 3) {
                    int month = Integer.parseInt(parts[1]);
                    int day = Integer.parseInt(parts[2]);
                    course.setDateDay(month * 100 + day);
                }
            }

            int rows = courseService.updateCourse(course);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "课程更新成功");
                result.put("data", course);
            } else {
                result.put("success", false);
                result.put("message", "课程更新失败或课程不存在");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "服务器错误：" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 根据ID删除课程
     */
    @GetMapping("/delete/{id}")
    public Map<String, Object> deleteCourse(@PathVariable Integer id) {
        Map<String, Object> result = new HashMap<>();
        try {
            int rows = courseService.deleteCourse(id);
            if (rows > 0) {
                result.put("success", true);
                result.put("message", "课程删除成功");
            } else {
                result.put("success", false);
                result.put("message", "课程删除失败或课程不存在");
            }
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "服务器错误：" + e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
}